// pages/zwyd/zwyd.js
const db=wx.cloud.database()
const app=getApp() 
var times=require('../../utils/times.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    list_id:'',
    rmbs:'',
    userinfo:'',
    openid:'',
    list:'',
		sjxx:'',
		lcItem:{},
		zwItem:{},
		date:new Date().toJSON().substring(0, 10),
		startDate:new Date().toJSON().substring(0, 10),
		timeType:0,
		allTimes:[
			{
				time:'8:00-9:00',
				type:1,
				disable:false,
			},
			{
				time:'9:00-10:00',
				type:2,
				disable:false,
			},
			{
				time:'10:00-11:00',
				type:3,
				disable:false,
			},
			{
				time:'11:00-12:00',
				type:4,
				disable:false,
			},
			{
				time:'13:00-14:00',
				type:5,
				disable:false,
			},
			{
				time:'14:00-15:00',
				type:6,
				disable:false,
			},
			{
				time:'15:00-16:00',
				type:7,
				disable:false,
			},
			{
				time:'16:00-17:00',
				type:8,
				disable:false,
			},
			{
				time:'17:00-18:00',
				type:9,
				disable:false,
			},
			{
				time:'19:00-20:00',
				type:10,
				disable:false,
			},
			{
				time:'20:00-21:00',
				type:11,
				disable:false,
			},
			{
				time:'21:00-22:00',
				type:12,
				disable:false
			}
		],
		choseTime:'',
		choseTimeType:''
  },
  /**
   * 生命周期函数--监听页面加载
   */
	bindDateChange:function(e){
		let that = this
    that.setData({
      date:e.detail.value
		})
		
		let params={
			zw_id:that.data.zwItem._id,
			choseDate:that.data.date
		}
		wx.showLoading({
			title: '数据重新生成',
		})
		wx.cloud.database().collection("tsgxz").where(params).get().then((res)=>{
				let xzList=res.data;
				let allTimes=that.data.allTimes
				if(xzList.length>0){
					xzList.forEach((it,index)=>{
						allTimes.map((item)=>{
									if(item.type == it.choseTimeType){
										item.disable=true
									}
									return item;
							})
					})
				}else{
					allTimes=[
						{
							time:'8:00-9:00',
							type:1,
							disable:false,
						},
						{
							time:'9:00-10:00',
							type:2,
							disable:false,
						},
						{
							time:'10:00-11:00',
							type:3,
							disable:false,
						},
						{
							time:'11:00-12:00',
							type:4,
							disable:false,
						},
						{
							time:'13:00-14:00',
							type:5,
							disable:false,
						},
						{
							time:'14:00-15:00',
							type:6,
							disable:false,
						},
						{
							time:'15:00-16:00',
							type:7,
							disable:false,
						},
						{
							time:'16:00-17:00',
							type:8,
							disable:false,
						},
						{
							time:'17:00-18:00',
							type:9,
							disable:false,
						},
						{
							time:'19:00-20:00',
							type:10,
							disable:false,
						},
						{
							time:'20:00-21:00',
							type:11,
							disable:false,
						},
						{
							time:'21:00-22:00',
							type:12,
							disable:false
						}]
				}
				that.setData({
					allTimes
				})
				wx.hideLoading()
		})
	},
	selectItem(e){
		let  timeType=e.currentTarget.dataset.index;
		let  choseTime = e.currentTarget.dataset.time;
		let disable = e.currentTarget.dataset.disable;
		if(!disable){
      this.setData({
				timeType: timeType,
				choseTime:choseTime,
				choseTimeType:timeType
			})
		}
	},
  onLoad: function (options) {
		var that = this
    var userinfo = wx.getStorageSync('userinfo')
    this.setData({
      list_id:options.list_id, 
      userinfo:userinfo,
      openid:userinfo._openid
		})

		wx.showLoading({
			title: '数据加载处理中',
			mask:true
		})
		
   wx.cloud.database().collection("tsgzw").where({'_id':options.list_id}).get().then(res=>{
			let result =res.data[0]
			that.setData({
				zwItem:result
			})
			console.log(that.data.zwItem)
			wx.cloud.database().collection("tsgwz").where({'_id':that.data.zwItem.lc}).get().then(res1=>{
				let result =res1.data[0]
				that.setData({
					lcItem:result
				})
				console.log(that.data.lcItem)
			})

			let params={
				zw_id:that.data.zwItem._id,
				choseDate:that.data.choseDate
			}
			wx.cloud.database().collection("tsgxz").where(params).get().then((res)=>{
					let xzList=res.data;
					let allTimes=that.data.allTimes
					xzList.forEach((it,index)=>{
						allTimes.map((item)=>{
                  if(item.type == it.choseTimeType){
										item.disable=true
									}
									return item;
							})
					})

					this.setData({
						allTimes
					})

					wx.hideLoading()
			})

		})
  },
  qrxz(){
		let that = this
		if(this.data.timeType==0){
			wx.showToast({
				mask:true,
				image: "../../images/warn.png", // 自定义图标的本地路径，image 的优先级高于 icon
				title: '请选择预约时间段',
				icon:  ''
			})
			return false;
		}

		let lcItem_id=that.data.lcItem._id
		let zwItem_id=that.data.zwItem._id
		console.log(lcItem_id+"-----"+zwItem_id)
    var id = app.globalData.openid
		 //时间充足，加入多选判断
		 /**
			* wx.showToast({
            title: '请勿多选',
            icon:  'error'
          })
		  */
      db.collection("tsgxz").add({
            data:{
              openid: app.globalData.openid,
              lcid: lcItem_id,
							zw_id: zwItem_id,
							zw_bh:that.data.zwItem.bh,
							choseDate:that.data.date,
							choseTime:that.data.choseTime,
							choseTimeType:that.data.choseTimeType,
							status:0,
              qt:0,
              _createTime:Date.parse(new Date())
            }
          }).then(res=>{
            wx.showToast({
              title: '选座成功',
              icon:'success',   
              duration:2000,
              success:()=>{
                db.collection("tsgzw").doc(this.data.zwItem._id).update({
                  data:{ status:0},
                 success:res=>{
                    wx.switchTab({
											url: '../grzx/grzx',
										})
							 	}
							 })
              }
            })
        })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})